package com.ruoyi.pay.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pay.domain.PayCoinUser;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.pay.domain.vo.PayCoinUserVO;
import com.ruoyi.pay.mapper.PayCoinUserMapper;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 趣币明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
public class PayCoinUserServiceImpl extends ServiceImpl<PayCoinUserMapper, PayCoinUser> implements IPayCoinUserService {
    
	@Autowired
    private PayCoinUserMapper payCoinUserMapper;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询趣币明细
     * 
     * @param id 趣币明细主键
     * @return 趣币明细
     */
    @Override
    public PayCoinUser selectPayCoinUserById(String id)
    {
        return payCoinUserMapper.selectPayCoinUserById(id);
    }

    /**
     * 查询趣币明细列表
     * 
     * @param payCoinUser 趣币明细
     * @return 趣币明细
     */
    @Override
    public List<PayCoinUser> selectPayCoinUserList(PayCoinUser payCoinUser)
    {
        return payCoinUserMapper.selectPayCoinUserList(payCoinUser);
    }

    /**
     * 新增趣币明细
     * 
     * @param payCoinUser 趣币明细
     * @return 结果
     */
    @Override
    public int insertPayCoinUser(PayCoinUser payCoinUser)
    {
        payCoinUser.setCreateTime(DateUtils.getNowDate());
        return payCoinUserMapper.insertPayCoinUser(payCoinUser);
    }

    /**
     * 修改趣币明细
     * 
     * @param payCoinUser 趣币明细
     * @return 结果
     */
    @Override
    public int updatePayCoinUser(PayCoinUser payCoinUser)
    {
        payCoinUser.setUpdateTime(DateUtils.getNowDate());
        return payCoinUserMapper.updatePayCoinUser(payCoinUser);
    }

    /**
     * 批量删除趣币明细
     * 
     * @param ids 需要删除的趣币明细主键
     * @return 结果
     */
    @Override
    public int deletePayCoinUserByIds(String[] ids)
    {
        return payCoinUserMapper.deletePayCoinUserByIds(ids);
    }

    /**
     * 删除趣币明细信息
     * 
     * @param id 趣币明细主键
     * @return 结果
     */
    @Override
    public int deletePayCoinUserById(String id)
    {
        return payCoinUserMapper.deletePayCoinUserById(id);
    }

	@Override
	public Integer getAmount() {
		Long userId = SecurityUtils.getUserId();
		Integer jia = baseMapper.getJiaNum(userId);
		if(jia == null){
			jia = 0;
		}
		Integer jian = baseMapper.getJianNum(userId);
		if(jian == null){
			jian = 0;
		}
		Integer balance = jia - jian;
		return balance;
	}

	@Override
	public Integer saveApply(PayCoinUserForm payCoinUserForm) {
		Integer jia = baseMapper.getJiaNum(payCoinUserForm.getUserId());
		if(jia == null){
			jia = 0;
		}
		Integer jian = baseMapper.getJianNum(payCoinUserForm.getUserId());
		if(jian == null){
			jian = 0;
		}
		Integer balance = jia - jian;
		if(payCoinUserForm.getCoinNum() > balance){
			// 趣豆不足
			return 0;
		}
		
		PayCoinUser coinUser = new PayCoinUser();
		coinUser.setUserId(payCoinUserForm.getUserId());
		coinUser.setCoinNum(payCoinUserForm.getCoinNum());
		coinUser.setCoinType("1");
		coinUser.setSourceId(payCoinUserForm.getSourceId());
		coinUser.setRemark(payCoinUserForm.getRemark());
		baseMapper.insert(coinUser);
		
		Integer coinNum = baseMapper.getCoinNumBySourceId(payCoinUserForm.getSourceId());
		return coinNum;
	}

	@Override
	public void savePayCoinUser(PayCoinUserForm payCoinUserForm) {
		if(payCoinUserForm.getCoinNum() < 1){
			return;
		}
		if(payCoinUserForm.getSourceId() == null){
			PayCoinUser coinUser = new PayCoinUser();
			coinUser.setUserId(payCoinUserForm.getUserId());
			coinUser.setCoinNum(payCoinUserForm.getCoinNum());
			coinUser.setCoinType(payCoinUserForm.getCoinType());
			coinUser.setRemark(payCoinUserForm.getRemark());
			baseMapper.insert(coinUser);
			return;
		}
		
		QueryWrapper<PayCoinUser> queryWrapper = new QueryWrapper<>();
		PayCoinUser entity = new PayCoinUser();
		entity.setSourceId(payCoinUserForm.getSourceId());
		entity.setUserId(payCoinUserForm.getUserId());
		queryWrapper.setEntity(entity);
		PayCoinUser coinUser = baseMapper.selectOne(queryWrapper);
		if(coinUser == null){
			coinUser = new PayCoinUser();
			coinUser.setUserId(payCoinUserForm.getUserId());
			coinUser.setCoinNum(payCoinUserForm.getCoinNum());
			coinUser.setCoinType(payCoinUserForm.getCoinType());
			coinUser.setSourceId(payCoinUserForm.getSourceId());
			coinUser.setRemark(payCoinUserForm.getRemark());
			baseMapper.insert(coinUser);
		}else{
			coinUser.setCoinNum(payCoinUserForm.getCoinNum());
			coinUser.setCoinType(payCoinUserForm.getCoinType());
			coinUser.setRemark(payCoinUserForm.getRemark());
			baseMapper.updateById(coinUser);
		}
	}

	@Override
	public void updateCoinUserBack(Long userId, String sourceId, String remark) {
		// 返还趣豆
		QueryWrapper<PayCoinUser> queryWrapper = new QueryWrapper<>();
		PayCoinUser entity = new PayCoinUser();
		entity.setUserId(userId);
		entity.setSourceId(sourceId);
		queryWrapper.setEntity(entity);
		PayCoinUser payCoinUser = baseMapper.selectOne(queryWrapper);
		if(payCoinUser != null){
			PayCoinUser coinUser = new PayCoinUser();
			coinUser.setCoinNum(payCoinUser.getCoinNum());
			coinUser.setCoinType("0");
			coinUser.setUserId(payCoinUser.getUserId());
			coinUser.setSourceId(payCoinUser.getSourceId());
			coinUser.setRemark(remark);
			baseMapper.insert(coinUser);
		}
	}

	@Override
	public TableDataInfo getCoinByUserId(PayCoinUser payCoinUser) {
		IPage<PayCoinUser> page = baseMapper.selectPageList(Condition.getPage(payCoinUser), payCoinUser);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public void savePartyPayCoinUser(PayCoinUserForm payCoinUserForm) {
		QueryWrapper<PayCoinUser> queryWrapper = new QueryWrapper<>();
		PayCoinUser entity = new PayCoinUser();
		entity.setSourceId(payCoinUserForm.getSourceId());
		entity.setUserId(payCoinUserForm.getUserId());
		entity.setIsPay("2");
		queryWrapper.setEntity(entity);
		PayCoinUser coinUser = baseMapper.selectOne(queryWrapper);
		if(coinUser == null){
			coinUser = new PayCoinUser();
			coinUser.setUserId(payCoinUserForm.getUserId());
			coinUser.setCoinNum(payCoinUserForm.getCoinNum());
			coinUser.setCoinType(payCoinUserForm.getCoinType());
			coinUser.setSourceId(payCoinUserForm.getSourceId());
			coinUser.setRemark(payCoinUserForm.getRemark());
			coinUser.setIsPay("2");
			baseMapper.insert(coinUser);
		}
	}

	@Override
	public TableDataInfo getCoinList(PayCoinUser payCoinUser) {
		SysUser user = sysUserService.getById(payCoinUser.getUserId());
		payCoinUser.setIsPay("1");
		if("1".equals(user.getIsStore())){
			payCoinUser.setIsPay("2");
		}
		IPage<PayCoinUserVO> page = baseMapper.getCoinList(Condition.getPage(payCoinUser), payCoinUser);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Integer getPayAmount() {
		Long userId = SecurityUtils.getUserId();
		Integer jia = baseMapper.getPayJiaNum(userId);
		if(jia == null){
			jia = 0;
		}
		Integer jian = baseMapper.getPayJianNum(userId);
		if(jian == null){
			jian = 0;
		}
		Integer balance = jia - jian;
		return balance;
	}

	@Override
	public List<PayCoinUser> getMyPayCoinList(PayCoinUser payCoinUser) {
		return baseMapper.getMyPayCoinList(payCoinUser);
	}

	@Override
	public TableDataInfo getMyCoinUserList(PayCoinUserVO payCoinUser) {
		IPage<PayCoinUserVO> page = baseMapper.getMyCoinUserList(Condition.getPage(payCoinUser), payCoinUser);
		for(PayCoinUserVO cu : page.getRecords()){
			Integer coinNum = cu.getCoinNum();
			cu.setCoinNum(coinNum/100);
			cu.setCoinMoney(BigDecimal.valueOf(coinNum).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP));
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public PayCoinUser getCoinUserBySrouceId(String sourceId) {
		QueryWrapper<PayCoinUser> queryWrapper = new QueryWrapper<>();
		PayCoinUser entity = new PayCoinUser();
		entity.setSourceId(sourceId);
		queryWrapper.setEntity(entity);
		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public Integer getMyCashMoney(PayCoinUserVO payCoinUser) {
		Integer cash = baseMapper.getMyCashMoney(payCoinUser);
		if(cash == null){
			cash = 0;
		}
		return cash;
	}
}
